package org.jeecg.modules.yw.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.yw.entity.FlPrice;
import org.jeecg.modules.yw.entity.MaterialTemplate;
import org.jeecg.modules.yw.entity.Order;
import org.jeecg.modules.yw.entity.PaperInfo;
import org.jeecg.modules.yw.entity.PriceRulesEnterprise;
import org.jeecg.modules.yw.mapper.MaterialTemplateMapper;
import org.jeecg.modules.yw.utils.DateUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class MaterialTemplateService extends ServiceImpl<MaterialTemplateMapper, MaterialTemplate>{
	@Resource
	private MaterialTemplateMapper materialTemplateMapper;
	@Resource
	private OrderService orderService;
	
	@Resource
	private PriceRulesService priceRuleService;
	
	@Resource
	private FlPriceService flPriceService;
	
	
	public List<MaterialTemplate> getAllMateralTemplateInfo() {
		QueryWrapper<MaterialTemplate> queryWrapper = new QueryWrapper<>();		
	    queryWrapper.orderByAsc("num");		
		List<MaterialTemplate> list = materialTemplateMapper.selectList(queryWrapper);			
		return list;
	}
	
	
	public List<MaterialTemplate> getMaterialTemplateList(String orderId) {
		 // 得到订单详情
		 Order order = orderService.getOrder(orderId);
		
		 String userId = order.getCreateUserId();
		
		 List<MaterialTemplate> list = getAllMateralTemplateInfo();
		
		 List<PriceRulesEnterprise> ruleList = priceRuleService.getAllRuleListByEnterpriseId(userId);
		 
		 Map<Integer, List<PriceRulesEnterprise>> map = priceRuleService.getAllRuluesByEnterpriseId(userId);
		 
		 
		 FlPrice flPrice = flPriceService.getAllInfoByEnterpriseId(userId);		 
		 if (flPrice == null) {
			 flPrice = flPriceService.getById(1);
		 }
		 
		 String dateStr = order.getDjFinishTime();
		 
		 if (StringUtils.isNotBlank(dateStr)) {
			 dateStr = DateUtils.flagMonthFirstDay(dateStr, "366");					 
		 } else {
			 dateStr = DateUtils.flagMonthFirstDay(DateUtils.getDateStr(), "366");				
		 }
		 
		 List<PaperInfo> paperList = order.getPageInfoList();
		 
		 String nqjSum = "0"; // 内墙胶
		 String nbljSum = "0"; // 内玻璃胶
		 String wbljSum = "0"; // 外玻璃胶
		 
		 String bhNqjSum = "0"; // 不含辅料内墙胶
		 String bhNbljSum = "0";
		 String bhWbljSum = "0";
		 String hflSum = "0";
		 
		 
		 String nqjSumPrice = "0"; // 内墙胶总价格
		 String nbljSumPrice = "0"; // 内玻璃胶总价格
		 String wbljSumPrice = "0"; // 外玻璃胶总价格
		 
		 String bhNqjSumPrice = "0"; // 不含辅料内墙胶总价格
		 String bhNbljSumPrice = "0";  // 不含辅料内玻璃胶总价格
		 String bhWbljSumPrice = "0"; //  不含辅料外玻璃胶总价格
		 
		 
		 for (PaperInfo obj : paperList) {
			 String priceOption = priceRuleService.getPriceAndOptionByRuleEnterprise(Integer.parseInt(obj.getType()), obj.getArea(), order.getFl(), order.getSfjj(), order.getJjlx(), map, flPrice);
			 if(StringUtils.isBlank(priceOption)){
				 continue;
			 }
			 String price = priceOption.split(",")[0];
			 String option = priceOption.split(",")[1];
			 String hflPrice = priceOption.split(",")[2];
			 String unitPrice = priceOption.split(",")[3];
			 String fixedPrice = priceOption.split(",")[4];
			 hflSum =  new BigDecimal(hflSum).add(new BigDecimal(hflPrice)).toString();
			 obj.setChoice(option);
			 obj.setPaperPrice(price);
			 obj.setUnitPrice(unitPrice);
			 obj.setFixedPrice(fixedPrice);
			 if (order.getFl() == 1) {
				 if (order.getJjlx().contains("3") && option.equals("C") && (Integer.parseInt(obj.getType()) == 1||Integer.parseInt(obj.getType()) == 2)) {
					 String ngjPrice = new BigDecimal(flPrice.getQtjhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 nqjSumPrice = new BigDecimal(nqjSumPrice).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
					 nqjSum = new BigDecimal(nqjSum).add(new BigDecimal(ngjPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("3") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String ngjPrice = new BigDecimal(flPrice.getNqtjhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 nqjSumPrice = new BigDecimal(nqjSumPrice).add(new BigDecimal(flPrice.getNqtjhflPrice())).toString();
					 nqjSum = new BigDecimal(nqjSum).add(new BigDecimal(ngjPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("3") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String ngjPrice = new BigDecimal(flPrice.getDqnqtjhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 nqjSumPrice = new BigDecimal(nqjSumPrice).add(new BigDecimal(flPrice.getDqnqtjhflPrice())).toString();
					 nqjSum = new BigDecimal(nqjSum).add(new BigDecimal(ngjPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("1") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String nbljPrice = new BigDecimal(flPrice.getNbljhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();
					 nbljSumPrice = new BigDecimal(nbljSumPrice).add(new BigDecimal(flPrice.getNbljhflPrice())).toString();
					 nbljSum = new BigDecimal(nbljSum).add(new BigDecimal(nbljPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("1") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String nbljPrice = new BigDecimal(flPrice.getDqnbljhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();
					 nbljSumPrice = new BigDecimal(nbljSumPrice).add(new BigDecimal(flPrice.getDqnbljhflPrice())).toString();
					 nbljSum = new BigDecimal(nbljSum).add(new BigDecimal(nbljPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("2") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String wbljPrice = new BigDecimal(flPrice.getWbljhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();
					 wbljSumPrice = new BigDecimal(wbljSumPrice).add(new BigDecimal(flPrice.getWbljhflPrice())).toString();
					 wbljSum = new BigDecimal(wbljSum).add(new BigDecimal(wbljPrice)).toString();
				 }
				 
				 
				 if (order.getJjlx().contains("2") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String wbljPrice = new BigDecimal(flPrice.getDqwbljhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();
					 wbljSumPrice = new BigDecimal(wbljSumPrice).add(new BigDecimal(flPrice.getDqwbljhflPrice())).toString();
					 wbljSum = new BigDecimal(wbljSum).add(new BigDecimal(wbljPrice)).toString();
				 }
				
				 
			 } else {
				 if (order.getJjlx().contains("3") && option.equals("C") && (Integer.parseInt(obj.getType()) == 1||Integer.parseInt(obj.getType()) == 2)) {
					 String bhngjPrice = new BigDecimal(flPrice.getQtjbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();		
					 bhNqjSumPrice = new BigDecimal(bhNqjSumPrice).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
					 bhNqjSum = new BigDecimal(bhNqjSum).add(new BigDecimal(bhngjPrice)).toString();
				 }

				 if (order.getJjlx().contains("3") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String bhngjPrice = new BigDecimal(flPrice.getNqtjbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 bhNqjSumPrice = new BigDecimal(bhNqjSumPrice).add(new BigDecimal(flPrice.getNqtjbhflPrice())).toString();
					 bhNqjSum = new BigDecimal(bhNqjSum).add(new BigDecimal(bhngjPrice)).toString();
				 }
				 
				 if (order.getJjlx().contains("3") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String bhngjPrice = new BigDecimal(flPrice.getDqnqtjbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 bhNqjSumPrice = new BigDecimal(bhNqjSumPrice).add(new BigDecimal(flPrice.getDqnqtjbhflPrice())).toString();
					 bhNqjSum = new BigDecimal(bhNqjSum).add(new BigDecimal(bhngjPrice)).toString();
				 }					 

				 if (order.getJjlx().contains("1") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String bhnbljPrice = new BigDecimal(flPrice.getNbljbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 bhNbljSumPrice = new BigDecimal(bhNbljSumPrice).add(new BigDecimal(flPrice.getNbljbhflPrice())).toString();
					 bhNbljSum = new BigDecimal(bhNbljSum).add(new BigDecimal(bhnbljPrice)).toString();
				 }				 
				 
				 if (order.getJjlx().contains("1") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String bhnbljPrice = new BigDecimal(flPrice.getNbljbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();
					 bhNbljSumPrice = new BigDecimal(bhNbljSumPrice).add(new BigDecimal(flPrice.getNbljbhflPrice())).toString();
					 bhNbljSum = new BigDecimal(bhNbljSum).add(new BigDecimal(bhnbljPrice)).toString();
				 }			 
				 
				 if (order.getJjlx().contains("2") && option.equals("C") && Integer.parseInt(obj.getType()) == 3) {
					 String bhwbljPrice = new BigDecimal(flPrice.getWbljbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 bhWbljSumPrice = new BigDecimal(bhWbljSumPrice).add(new BigDecimal(flPrice.getWbljbhflPrice())).toString();
					 bhWbljSum = new BigDecimal(bhWbljSum).add(new BigDecimal(bhwbljPrice)).toString();
				 }				 
				 
				 if (order.getJjlx().contains("2") && option.equals("C") && Integer.parseInt(obj.getType()) == 4) {
					 String bhwbljPrice = new BigDecimal(flPrice.getDqwbljbhflPrice()).multiply(new BigDecimal(obj.getArea())).toString();	
					 bhWbljSumPrice = new BigDecimal(bhWbljSumPrice).add(new BigDecimal(flPrice.getDqwbljbhflPrice())).toString();
					 bhWbljSum = new BigDecimal(bhWbljSum).add(new BigDecimal(bhwbljPrice)).toString();
				 }
				 
			 }
		 }
		 
		 List<MaterialTemplate> newList = new ArrayList<>();		 
		 for (MaterialTemplate obj : list) {
			if (obj.getNum() == 1) {
				obj.setArea(order.getOwnerAddress());
			} else if (obj.getNum() == 2) {
				obj.setArea(order.getOwnerPhone());
			} else if (obj.getNum() == 3) {
				obj.setArea(order.getYyTime());
			} else if (obj.getNum() == 4) {
				obj.setArea(dateStr);
			}
			Integer num = obj.getNum();
			if (num >= 6 && num <= 24) {
				//String unitPrice = getUnitPrice(obj.getType(), obj.getChoice(), ruleList);				
				for (PaperInfo paper : paperList) {
					if (obj.getType() == Integer.parseInt(paper.getType()) && obj.getChoice().equals(paper.getChoice())) {
						obj.setXj(paper.getPaperPrice());
						obj.setArea(paper.getArea());
						obj.setPrice(paper.getUnitPrice());
						if (obj.getChoice().equals("C")) {
							String xj = new BigDecimal(paper.getArea()).multiply(new BigDecimal(paper.getUnitPrice())).setScale(2, RoundingMode.HALF_UP).toString().toString();						
							obj.setXj(xj);
						} else {
							if (obj.getType() == 5 || obj.getType() == 6 || obj.getType() == 7 || obj.getType() == 8 || obj.getType() == 9) {
								String xj = new BigDecimal(paper.getArea()).multiply(new BigDecimal(paper.getUnitPrice())).setScale(2, RoundingMode.HALF_UP).toString().toString();						
								obj.setXj(xj);
							} else {							
								obj.setXj(paper.getFixedPrice());
							}
						}
						break;
					}
				}
			}	
			if (num == 25) {
				String flStr = order.getFl() == 1?"是":"否";
				obj.setRemark("在新建订单里选择是否含辅料选择" + flStr);
				obj.setPrice(flPrice.getHflPrice());
				obj.setXj(hflSum);
			}
			if (num == 26) {
				if (order.getCc() == 1) {
					obj.setXj(order.getGbdkPrice());
					obj.setArea(String.valueOf(order.getCcNum()==null?"0":order.getCcNum()));
				}
				obj.setPrice(flPrice.getGbdkPrice());
			}
			if (num == 27) {
				if (order.getCcfs() == 1) {
					obj.setXj(order.getGfccPrice());
					obj.setArea(String.valueOf(order.getCcfsNum()==null?"0":order.getCcfsNum()));
				}
				obj.setPrice(flPrice.getGfccPrice());
			}
			if (num == 28) {
				if (order.getCpby() != null && order.getCpby().contains("4")) {
					obj.setXj(order.getCpbyPrice());
				}
				obj.setPrice(flPrice.getCpbyPrice());
			}
			if (num == 29) {
				if (order.getWgsc() != null && order.getWgsc() == 1) {
					obj.setXj(order.getWgscPrice());
					obj.setArea(String.valueOf(order.getWgscNum()==null?"0":order.getWgscNum()));
				}
				obj.setPrice(flPrice.getWgscPrice());
			}
			if (num == 30) {
				if (order.getSghj() != null && order.getSghj() == 0) {
					obj.setXj(order.getSghjPrice());
				}
				obj.setPrice(flPrice.getSghjPrice());
			}
			if (num == 31) {
				//obj.setPrice(flPrice.getNqtjhflPrice());
				obj.setPrice(nqjSumPrice);
				obj.setXj(nqjSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}
			if (num == 32) {
				//obj.setPrice(flPrice.getNbljhflPrice());
				obj.setPrice(nbljSumPrice);
				obj.setXj(nbljSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}
			if (num == 33) {
				//obj.setPrice(flPrice.getWbljhflPrice());
				obj.setPrice(wbljSumPrice);
				obj.setXj(wbljSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}
			if (num == 34) {
				//obj.setPrice(flPrice.getNqtjbhflPrice());
				obj.setPrice(bhNqjSumPrice);
				obj.setXj(bhNqjSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}
			if (num == 35) {
				//obj.setPrice(flPrice.getNbljbhflPrice());
				obj.setPrice(bhNbljSumPrice);
				obj.setXj(bhNbljSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}
			if (num == 36) {
				//obj.setPrice(flPrice.getWbljbhflPrice());
				obj.setPrice(bhWbljSumPrice);
				obj.setXj(bhWbljSum);
				if (obj.getXj() != null && !obj.getXj().equals("0")) {
					String area = new BigDecimal(obj.getXj()).divide(new BigDecimal(obj.getPrice())).setScale(2, RoundingMode.HALF_UP).toString();
					obj.setArea(area);
				}
			}			
			if (num == 40) {
				obj.setXj(order.getTotalPrice());
			}
		 }				 
		 ///System.out.println("json="+JSON.toJSONString(list).toString());		 
		 for (MaterialTemplate obj : list) {
			 if (obj.getNum() >= 31 && obj.getNum() <= 40) {
				 if (!obj.getXj().trim().equals("0")) {
					 newList.add(obj);
				 }
			 } else {		
				 if (obj.getNum() != 25) {
					 if (obj.getNum() <= 4) {
						 newList.add(obj);
					 } else {
						 if(StringUtils.isNotBlank(obj.getXj().trim()) && !obj.getXj().trim().equals("0.00")) {
							 newList.add(obj);
						 }
					 }					 
				 }				
			 }			 
		 }		
		 return newList;
	}
	
	/**
	 * 得到单价
	 * @param type
	 * @param choice
	 * @param ruleList
	 * @return
	 */
	public String getUnitPrice(Integer type, String choice,  List<PriceRulesEnterprise>  ruleList) {
		String unitPrice = "";
		for (PriceRulesEnterprise obj : ruleList) {
			if (obj.getType() == type && choice.equals(obj.getChoice())) {
				unitPrice = obj.getPrice();
				break;
			}
		}			
		return unitPrice;
	}	
}
